package cn.lyf.myblog.shiro;

import java.util.*;

import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class ShiroConfig {

    @Bean("UserRealm")
    public UserRealm getRealm() {
        return new UserRealm();
    }

    @Bean("mySecurityManager")
    public DefaultWebSecurityManager getMySecurityManager(@Qualifier("UserRealm") UserRealm realm) {
        DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
        securityManager.setRealm(realm);
        return securityManager;
    }

    @Bean
    public ShiroFilterFactoryBean getMyFactoryBean(
            @Qualifier("mySecurityManager") DefaultWebSecurityManager securityManager) {
        ShiroFilterFactoryBean shiroFactoryBean = new ShiroFilterFactoryBean();
        shiroFactoryBean.setSecurityManager(securityManager);
        // 过滤
        Map<String, String> filterMap = new LinkedHashMap<>();
        filterMap.put("/user/*", "user");
        filterMap.put("/content/*", "authc");
        filterMap.put("/res/**","anon");
        filterMap.put("/check","anon");
        filterMap.put("/send","anon");
        shiroFactoryBean.setLoginUrl("/toLogin");

        shiroFactoryBean.setFilterChainDefinitionMap(filterMap);
        return shiroFactoryBean;
    }

}
